<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>coordinate.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_coordinate.js.html">coordinate.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for representing two-dimensional positions.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.math.Coordinate&#39;);
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>/**
<a name="line24"></a> * Class for representing coordinates and positions.
<a name="line25"></a> * @param {number} opt_x Left, defaults to 0.
<a name="line26"></a> * @param {number} opt_y Top, defaults to 0.
<a name="line27"></a> * @constructor
<a name="line28"></a> */
<a name="line29"></a>goog.math.Coordinate = function(opt_x, opt_y) {
<a name="line30"></a>  /**
<a name="line31"></a>   * X-value
<a name="line32"></a>   * @type {number}
<a name="line33"></a>   */
<a name="line34"></a>  this.x = goog.isDef(opt_x) ? opt_x : 0;
<a name="line35"></a>
<a name="line36"></a>  /**
<a name="line37"></a>   * Y-value
<a name="line38"></a>   * @type {number}
<a name="line39"></a>   */
<a name="line40"></a>  this.y = goog.isDef(opt_y) ? opt_y : 0;
<a name="line41"></a>};
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Returns a new copy of the coordinate.
<a name="line46"></a> * @return {goog.math.Coordinate} A clone of this coordinate.
<a name="line47"></a> */
<a name="line48"></a>goog.math.Coordinate.prototype.clone = function() {
<a name="line49"></a>  return new goog.math.Coordinate(this.x, this.y);
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>if (goog.DEBUG) {
<a name="line54"></a>  /**
<a name="line55"></a>   * Returns a nice string representing the coordinate.
<a name="line56"></a>   * @return {string} In the form (50, 73).
<a name="line57"></a>   */
<a name="line58"></a>  goog.math.Coordinate.prototype.toString = function() {
<a name="line59"></a>    return &#39;(&#39; + this.x + &#39;, &#39; + this.y + &#39;)&#39;;
<a name="line60"></a>  };
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Compares coordinates for equality.
<a name="line66"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line67"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line68"></a> * @return {boolean} True iff the coordinates are equal, or if both are null.
<a name="line69"></a> */
<a name="line70"></a>goog.math.Coordinate.equals = function(a, b) {
<a name="line71"></a>  if (a == b) {
<a name="line72"></a>    return true;
<a name="line73"></a>  }
<a name="line74"></a>  if (!a || !b) {
<a name="line75"></a>    return false;
<a name="line76"></a>  }
<a name="line77"></a>  return a.x == b.x &amp;&amp; a.y == b.y;
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Returns the distance between two coordinates.
<a name="line83"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line84"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line85"></a> * @return {number} The distance between {@code a} and {@code b}.
<a name="line86"></a> */
<a name="line87"></a>goog.math.Coordinate.distance = function(a, b) {
<a name="line88"></a>  var dx = a.x - b.x;
<a name="line89"></a>  var dy = a.y - b.y;
<a name="line90"></a>  return Math.sqrt(dx * dx + dy * dy);
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Returns the squared distance between two coordinates. Squared distances can
<a name="line96"></a> * be used for comparisons when the actual value is not required.
<a name="line97"></a> *
<a name="line98"></a> * Performance note: eliminating the square root is an optimization often used
<a name="line99"></a> * in lower-level languages, but the speed difference is not nearly as
<a name="line100"></a> * pronounced in JavaScript (only a few percent.)
<a name="line101"></a> *
<a name="line102"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line103"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line104"></a> * @return {number} The squared distance between {@code a} and {@code b}.
<a name="line105"></a> */
<a name="line106"></a>goog.math.Coordinate.squaredDistance = function(a, b) {
<a name="line107"></a>  var dx = a.x - b.x;
<a name="line108"></a>  var dy = a.y - b.y;
<a name="line109"></a>  return dx * dx + dy * dy;
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Returns the difference between two coordinates as a new
<a name="line115"></a> * goog.math.Coordinate.
<a name="line116"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line117"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line118"></a> * @return {goog.math.Coordinate} A Coordinate representing the difference
<a name="line119"></a> *     between {@code a} and {@code b}.
<a name="line120"></a> */
<a name="line121"></a>goog.math.Coordinate.difference = function(a, b) {
<a name="line122"></a>  return new goog.math.Coordinate(a.x - b.x, a.y - b.y);
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Returns the sum of two coordinates as a new goog.math.Coordinate.
<a name="line128"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line129"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line130"></a> * @return {goog.math.Coordinate} A Coordinate representing the sum of the two
<a name="line131"></a> *     coordinates.
<a name="line132"></a> */
<a name="line133"></a>goog.math.Coordinate.sum = function(a, b) {
<a name="line134"></a>  return new goog.math.Coordinate(a.x + b.x, a.y + b.y);
<a name="line135"></a>};
</pre>


</body>
</html>
